﻿
namespace EasyNet.Solr.Impl
{
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Serialization.Json;
    using System.Text;
    using System.Xml;
    using System.Xml.Linq;

    using Commons;

    public class JsonGroupQueryResultsParser<T> : ISolrResponseParser<string, IList<GroupQueryResults<T>>>
    {
        private readonly IObjectDeserializer<T> objectDeserialize;

        public JsonGroupQueryResultsParser(IObjectDeserializer<T> objectDeserialize)
        {
            this.objectDeserialize = objectDeserialize;

        }

        public IList<GroupQueryResults<T>> Parse(string result)
        {
            if (string.IsNullOrEmpty(result))
            {
                return null;
            }

            var bytes = Encoding.UTF8.GetBytes(result);

            using (var reader = JsonReaderWriterFactory.CreateJsonReader(bytes, XmlDictionaryReaderQuotas.Max))
            {
                var xmlDoc = XDocument.Load(reader);
                var grouped = xmlDoc.Element("grouped");

                if (grouped == null)
                {
                    return null;
                }

                var queryResults = new List<GroupQueryResults<T>>(grouped.Elements().Count());

                foreach (var element in grouped.Elements())
                {
                    var groupName = element.Name.LocalName;
                    var groupQueryResults = new GroupQueryResults<T>() { GroupName = groupName, Matches = int.Parse(element.Element("matches").Value) };
                    var groupsElement = element.Element("groups");

                    if (groupsElement != null)
                    {
                        groupQueryResults.Groups = new List<GroupQueryResult<T>>(groupsElement.Elements().Count());

                        foreach (var groupElement in groupsElement.Elements())
                        {
                            var groupQueryResult = new GroupQueryResult<T>() { GroupValue = groupElement.Element("groupValue").Value };
                            var doclistElement = groupElement.Element("doclist");
                            var solrDocumentList = new SolrDocumentList();

                            if (doclistElement != null)
                            {
                                var numFoundElement = doclistElement.Element("numFound");

                                if (numFoundElement != null)
                                {
                                    solrDocumentList.NumFound = long.Parse(numFoundElement.Value);
                                }

                                var startElement = doclistElement.Element("start");

                                if (startElement != null)
                                {
                                    solrDocumentList.Start = long.Parse(startElement.Value);
                                }

                                var maxScoreElement = doclistElement.Element("maxScore");

                                if (maxScoreElement != null)
                                {
                                    solrDocumentList.MaxScore = float.Parse(maxScoreElement.Value);
                                }

                                var docsElement = doclistElement.Element("docs");

                                if (docsElement != null)
                                {
                                    foreach (var docElement in docsElement.Elements())
                                    {
                                        var solrDocument = new SolrDocument();

                                        foreach (var itemElement in docElement.Elements())
                                        { 
                                            
                                        }
                                    }
                                }
                            }

                            groupQueryResults.Groups.Add(groupQueryResult);
                        }
                    }

                    queryResults.Add(groupQueryResults);
                }

                return queryResults;
            }
        }
    }
}
